<?php

class CString
{

		function cut ($string, $length, $padd = '...') { 
                $string = strip_tags($string); 
                $returnString = ''; 
                if (self::strlen($string) <= $length) 
                { 
                        $returnString = $string; 
                } 
                else 
                { 
                        $returnString = self::substr($string, 0, $length-self::strlen($padd)).$padd; 
                } 
                return $returnString; 
        }

	public function truncateByWords($phrase, $max_words = 5){
	   $phrase_array = explode(' ',$phrase);
	   if(count($phrase_array) > $max_words && $max_words > 0)
	      $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...';
	   return $phrase;
	}
	
	public function truncate($text, $length, $suffix = '&hellip;', $isHTML = true){
		$i = 0;
		$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
		$tags = array();
		if($isHTML){
			preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
			foreach($m as $o){
				if($o[0][1] - $i >= $length)
					break;
				$t = self::substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
				// test if the tag is unpaired, then we mustn't save them
				if($t[0] != '/' && (!isset($simpleTags[$t])))
					$tags[] = $t;
				elseif(end($tags) == self::substr($t, 1))
					array_pop($tags);
				$i += $o[1][1] - $o[0][1];
			}
		}
		
		// output without closing tags
		$output = self::substr($text, 0, $length = min(self::strlen($text),  $length + $i));
		// closing tags
		$output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
		
		// Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
		$pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
		// Append closing tags to output
		$output.=$output2;

		// Get everything until last space
		$one = self::substr($output, 0, $pos);
		// Get the rest
		$two = self::substr($output, $pos, (self::strlen($output) - $pos));
		// Extract all tags from the last bit
		preg_match_all('/<(.*?)>/s', $two, $tags);
		// Add suffix if needed
		if (self::strlen($text) > $length) { $one .= $suffix; }
		// Re-attach tags
		$output = $one . implode($tags[0]);
		
		return $output;
	}
    
    static function strlen($str){
    	if(function_exists('mb_strlen'))
    		return mb_strlen($str, 'UTF-8');

    	return strlen($str);
    }
    
    static function substr($str, $start, $length = null){
    	if(function_exists('mb_substr'))
    		return mb_substr($str, $start, $length, 'UTF-8');

    	return substr($str, $start, $length);
    }
    
    static function strpos($haystack , $needle, $offset = null){
    	if(function_exists('mb_strrpos'))
    		return mb_strpos($haystack , $needle, $offset, 'UTF-8');

    	return strpos($haystack , $needle, $offset);
    }
    
    static function strrpos($haystack , $needle, $offset = null){
    	if(function_exists('mb_strrpos'))
    		return mb_strrpos($haystack , $needle, $offset, 'UTF-8');

    	return strrpos($haystack , $needle, $offset);
    }
    
    static function strtolower($str){
    	if(function_exists('mb_strtolower'))
    		return mb_strtolower($str, 'UTF-8');

    	return strtolower($str);
    }
    
}